// import FormData from "../../utils/Formdata";
Page({
    data: {
        modalHidden: true,
        good: {},
        price:[]
    },
    onShareAppMessage() {
        let that = this
        let good =that.data.good
        console.log(good)
        const promise = new Promise(resolve => {
            setTimeout(() => {
                resolve({
                    title:good.name
                })
            }, 2000)
        })
        let  path = `/pages/goods_list/index?content=${good.content}&name=${good.name}&price=${good.price}&img=${good.picture}`
        return {
            title: good.name,
            path:path,
            imageUrl:good.picture,
        }
    },
    onLoad(option) {
        let that = this
        const eventChannel = this.getOpenerEventChannel()
        if (JSON.stringify(eventChannel) === '{}') {
            let option = getCurrentPages()[0].options
            that.setData({
                good:{
                    content:option.content,
                    price:option.price,
                    name:option.name,
                    picture:option.img
                },
                price:parseInt(option.price).toFixed(2).split('.')
            })
            console.log(option)
        } else {
            eventChannel.on('acceptDataFromOpenerPage', function (data) {
                that.setData({
                    good: data.data,
                    price: data.data.price.toFixed(2).split('.')
                })

            })
        }

    },
    moveOrder(){
        let token  = wx.getStorageSync('token')
        let that = this
        let formdata = '\r\n--XXX' +
            '\r\nContent-Disposition: form-data; name="goodId"' +
            '\r\n' +
            '\r\n' + that.data.good.id+
            '\r\n--XXX' +
            '\r\nContent-Disposition: form-data; name="num"' +
            '\r\n' +
            '\r\n1' +
            '\r\n--XXX--'
        wx.request({
            url: "https://www.cbing.icu:8095/wxUser/addToShopCart",
            method: 'POST',
            header: {
                'content-type': 'multipart/form-data;boundary=XXX',
                'Authorization':token
            },
            data: formdata,
            success(res) {
                if (res.data.status == -1) {
                    wx.redirectTo({url: '../self/index'})
                    wx.showToast({
                        icon: 'none',
                        title: "请先登录！",
                        duration: 2000
                    })
                } else {
                    if (res.data.code === 200) {
                        wx.setStorageSync('imm',true)
                        wx.setStorageSync('cart',[that.data.good])
                        wx.setStorageSync('total',that.data.good.price*100)
                        wx.redirectTo({
                            url:'../order/index'
                        })
                        // wx.redirectTo({url: '../category/index'})
                    }
                }
            }
        })

    },
    addToCart() {
        let that = this
        let token = wx.getStorageSync("token")
        wx.showModal({
            title: 'Tips',
            content: '您确定要将此商品加入购物车吗',
            confirmText: '确定',
            cancelText: '取消',
            success: function (res) {
                if (res.cancel) {

                } else {
                    let formdata = '\r\n--XXX' +
                        '\r\nContent-Disposition: form-data; name="goodId"' +
                        '\r\n' +
                        '\r\n' + that.data.good.id+
                        '\r\n--XXX' +
                        '\r\nContent-Disposition: form-data; name="num"' +
                        '\r\n' +
                        '\r\n1' +
                        '\r\n--XXX--'

                    console.log(that.data.good)
                    wx.request({

                        url: "https://www.cbing.icu:8095/wxUser/addToShopCart",
                        method: 'POST',
                        header: {
                            'content-type': 'multipart/form-data;boundary=XXX',
                            'Authorization':token
                        },
                        data: formdata,
                        success(res) {
                            console.log(res)
                            if (res.data.status == -1) {
                                wx.redirectTo({url: '../self/index'})
                                wx.showToast({
                                    icon: 'none',
                                    title: "请先登录！",
                                    duration: 2000
                                })
                            } else {
                                if (res.data.code == 200) {
                                    // wx.redirectTo({url: '../category/index'})
                                }
                                wx.showToast({
                                    icon: 'none',
                                    title: res.data.message,
                                    duration: 2000
                                })
                            }


                        }
                    })
                }
            }
        })
    },
    buttonTap: function () {

    },
    share(){
        wx.showShareMenu({
            withShareTicket: true,
            menus: ['shareAppMessage', 'shareTimeline']
        })
    }

})